<?php

namespace app\admin\model;

use think\Model;

class ExaminationPaper extends Model
{
    protected $autoWriteTimestamp = true;

    public static function getList()
    {
        $where = [];
        $limit = input('get.limit');
        if (input('get.title')) $where[] = ['title', 'like', '%' . input('get.title') . '%'];
        $list = self::alias('a')
            ->join('examination_category b', 'b.id = a.cid', 'left')
            ->order('a.id', 'desc')
            ->field('a.*, b.title as category_title')
            ->where($where)
            ->paginate($limit);
        foreach ($list as $key => $item) {
            $data = ExaminationQuestion::getCountAndTotalByPid($item['id']);
            $list[$key]['numbers'] = $data['numbers'] ?? 0;
            $list[$key]['scores'] = $data['scores'] ?? 0;
            $list[$key]['difficulty'] = (int)ExaminationQuestion::getAvgDifficultyByPid($item['id']);
        }
        return ['code' => 0, 'data' => $list->items(), 'extend' => ['count' => $list->total(), 'limit' => $limit]];
    }
}